package com.leapp.sk.ui.activity.archives

import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.view.View
import cc.vv.lkbasecomponent.util.LKDateTimeUtil
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.example.mvp.view.TimePicker.OnDateSetListener
import com.example.mvp.view.TimePicker.TimePickerDialog
import com.example.mvp.view.imgselector.ImageSelector
import com.example.util.addYear
import com.leapp.sk.R
import com.leapp.sk.bean.BabyInfoEntity
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.inter.ImageHeadPathInterface
import com.leapp.sk.util.HeadImageCropUtil
import com.example.util.finallist.BABY_INFO
import com.example.util.finallist.CODE_202
import com.leapp.sk.viewdelegate.archives.CompleteBabyInfoDelegate
import java.io.File

/**
 * 完善宝贝信息
 * @author  chaohaipeng  on 2018/8/7.
 */
class CompleteBabyInfoActivity : BaseActivityMVP<CompleteBabyInfoDelegate>(), OnDateSetListener, ImageHeadPathInterface {
    /**
     * 时间选择器
     */
    private var mPickerDialog: TimePickerDialog? = null
    override fun initDate() {
        initSeleteDate()
    }

    override fun bindEvenListener() {
        viewDelegate.setOnClickListener(View.OnClickListener {
            viewDelegate.initSexDialog()
        }, R.id.tv_acbi_sex)

        viewDelegate.setOnClickListener(View.OnClickListener {
            mPickerDialog?.show(fragmentManager, "info")
        }, R.id.tv_acbi_birth_date)
        viewDelegate.setOnClickListener(View.OnClickListener {
            pickImage()
        }, R.id.iv_acbi_head)

        viewDelegate.setOnClickListener(View.OnClickListener {
            if(!TextUtils.isEmpty( viewDelegate.nameCheck())&&!TextUtils.isEmpty( viewDelegate.sexCheck())&&
                    !TextUtils.isEmpty( viewDelegate.birthCheck())){
                val intent= Intent(this, BabyArchivesActivity::class.java)
                val babyInfoEntity = BabyInfoEntity()
                babyInfoEntity.babyName=viewDelegate.nameCheck()
                babyInfoEntity.babySex=viewDelegate.sexCheck()
                babyInfoEntity.babyBirth= viewDelegate.birthCheck()
                intent.putExtra(BABY_INFO,babyInfoEntity)
                setResult(CODE_202,intent)
                finish()
            }

        }, R.id.tv_acbi_next)
    }
    /**
     * 头像选择
     */
    private fun pickImage() {
        ImageSelector.getInstance()
                .setSelectModel(ImageSelector.MULTI_MODE)
                .setMaxCount(1)
                .setGridColumns(3)
                .setShowCamera(true)
                .startSelect(this)
    }
    override fun getDelegateClass(): Class<CompleteBabyInfoDelegate> {
      return  CompleteBabyInfoDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun initView(p0: Bundle?) {
        viewDelegate.setTitle(resources.getString(R.string.string_info_title))
    }

    override fun onDateSet(timePickerView: TimePickerDialog?, millseconds: Long) {
        val timeData = LKDateTimeUtil.formatDateTime(millseconds, "yyyy-MM-dd")
        viewDelegate.setTimeData(timeData)
    }

    override fun getImageHeadFile(file: File?) {
        viewDelegate.setHeadData(file?.absolutePath)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        HeadImageCropUtil.getResultHeadFile(requestCode, resultCode, data, this, this)
    }
    /**
     * 初始化时间选择器
     */
    private fun initSeleteDate() {
        var tenYears = addYear(-5)
        var endYears = addYear(5)
        mPickerDialog = TimePickerDialog.Builder()
                .setCallBack(this)
                .setCyclic(false)
                .setMinMillseconds(tenYears)
                .setThemeColor(ContextCompat.getColor(this, R.color.color_E0E0E0))
                .setTitleStringId(resources.getString(R.string.string_time_chose_title))
                .setWheelItemTextNormalColor(ContextCompat.getColor(this, R.color.color_A3A5A8))
                .setWheelItemTextSelectorColor(ContextCompat.getColor(this, R.color.color_474747))
                .setWheelItemTextSize(15)
                .setMaxMillseconds(endYears)
                .setVisiblesYMD(true)
                .setCurrentMillseconds(System.currentTimeMillis())
                .build()
    }
}